<?php
    // product cat model
    class ProcatModel
    {
        protected $tableName = "product_cat";
        public function __construct(PDO $pdo) {
            $this->pdo = $pdo;
        }
        function getNameCatProductByID($catID)
        {
            $sql = "SELECT NAME_CAT,ID_CAT FROM product_cat where ID_CAT = $catID LIMIT 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;
        }          
        function getNameCatProduct()
        {
            $sql = "SELECT NAME_CAT,ID_CAT FROM product_cat ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function getCatProductByID($catID)
        {
            $sql = "SELECT * FROM product_cat where ID_CAT = $catID LIMIT 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;
        }                       
        function getAllCatProduct()
        {
            $sql = "SELECT * FROM product_cat";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function getCatProductOther($catID)
        {
            $sql = "SELECT * FROM product_cat where ID_CAT <> $catID";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }        
        function insertCatProduct($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAME_CAT'])));
            $sql = "INSERT INTO product_cat (
                IDPARENT_CAT,
                NAME_CAT,
                NAMESEO_CAT,
                NAMEURL_CAT,
                DES_CAT,
                KEYWORD_CAT,
                SUMARYSEO_CAT,
                ICON_CAT,
                BACKG_CAT,
                ORDER_CAT,
                LANG_CAT,
                ISHOME_CAT,
                STATUS_CAT
                ) 
                VALUES (
                :IDPARENT_CAT,
                :NAME_CAT,
                :NAMESEO_CAT,
                :NAMEURL_CAT,
                :DES_CAT,
                :KEYWORD_CAT,
                :SUMARYSEO_CAT,
                :ICON_CAT,
                :BACKG_CAT,
                :ORDER_CAT,
                :LANG_CAT,
                :ISHOME_CAT,
                :STATUS_CAT
                )";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array(
                                ':IDPARENT_CAT'     =>$arrayCat['IDPARENT_CAT'],
                                ':NAME_CAT'         =>$arrayCat['NAME_CAT'],
                                ':NAMESEO_CAT'      =>$arrayCat['NAMESEO_CAT'],
                                ':NAMEURL_CAT'      =>$nameUrlSeo,
                                ':DES_CAT'          =>$arrayCat['DES_CAT'],
                                ':KEYWORD_CAT'      =>$arrayCat['KEYWORD_CAT'],
                                ':SUMARYSEO_CAT'    =>$arrayCat['SUMARYSEO_CAT'],
                                ':ICON_CAT'         =>$arrayCat['ICON_CAT'],
                                ':BACKG_CAT'        =>$arrayCat['BACKG_CAT'],
                                ':ORDER_CAT'        =>0,
                                ':LANG_CAT'         =>1,
                                ':ISHOME_CAT'       =>isset($arrayCat['ISHOME_CAT'])?1:0,
                                ':STATUS_CAT'       =>isset($arrayCat['STATUS_CAT'])?1:0,                         
                        ));
                            
            return true;
        }
        
        function editCatProduct($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAME_CAT'])));
            $sql = "UPDATE product_cat 
                    SET IDPARENT_CAT=?, NAME_CAT=?, NAMESEO_CAT=?, NAMEURL_CAT=?, DES_CAT=?,KEYWORD_CAT=?,
                    SUMARYSEO_CAT=?, ICON_CAT=?, BACKG_CAT=?, ISHOME_CAT=?, STATUS_CAT=?
            		WHERE ID_CAT=?";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array($arrayCat['IDPARENT_CAT'],$arrayCat['NAME_CAT'],$arrayCat['NAMESEO_CAT'],$nameUrlSeo,
            $arrayCat['DES_CAT'],$arrayCat['KEYWORD_CAT'],$arrayCat['SUMARYSEO_CAT'],$arrayCat['ICON_CAT'],
            $arrayCat['BACKG_CAT'],isset($arrayCat['ISHOME_CAT'])?1:0,isset($arrayCat['STATUS_CAT'])?1:0,$arrayCat['ID_CAT']));               
            return true;
        }
        function checkAllChildCat($ID_CAT){
            $sql = "SELECT ID_CAT product_cat WHERE IDPARENT_CAT=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function checkAllProCat($ID_CAT){
            $sql = "SELECT ID_PRODUCT set_products WHERE IDCAT_PRODUCT=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;
        }
        function deleteCat($ID_CAT)
        {
            $sql = "DELETE FROM `product_cat` WHERE ID_CAT=$ID_CAT ";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();            
        }   
    }
?>